package tests;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

import junit.framework.TestCase;

import org.junit.Test;

public class DBReaderTestMultiple extends TestCase {
	private Connection db;
	private int nOfCalls, currentCall;
	
	public DBReaderTestMultiple()  {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			this.db=DriverManager.getConnection("jdbc:mysql://alarcosj.esi.uclm.es:3306/bancoupm", "alumnodis", "alumnodis");
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			DataOutputStream dos=new DataOutputStream(new FileOutputStream("/private/var/folders/59/5rlw8d8s2p37hv3d7dz0q3qr0000gn/T/counter.counter"));
			dos.writeInt(1);
			dos.close();
			this.currentCall=1;
			DataInputStream dis=new DataInputStream(new FileInputStream("/var/folders/59/5rlw8d8s2p37hv3d7dz0q3qr0000gn/T/" +
					"int java.io.InputStream.read(byte[], int, int).jp"));
			this.nOfCalls=dis.readInt();
			System.out.println(nOfCalls);
			dis.close();
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	@Test
	public void testGetNumberOfOperations() {
		while (currentCall<this.nOfCalls) {
			try {
				int nom=0;
				String SQL="Select count(*) from Movimiento";
				PreparedStatement p=this.db.prepareStatement(SQL);
				ResultSet r=p.executeQuery();
				if (r.next()) {
					nom=r.getInt(1);
				}
				p.close();
				System.out.println(this.currentCall + "--> nom=" + nom);
				assertTrue("Expected 571", nom==571);
			} catch (Exception e) {
				System.out.println("Unexpected exception: " + e.toString());
			}
			this.currentCall++;
			try {
				DataOutputStream dos=new DataOutputStream(new FileOutputStream("/private/var/folders/59/5rlw8d8s2p37hv3d7dz0q3qr0000gn/T/counter.counter"));
				dos.writeInt(this.currentCall);
				dos.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			System.in.read();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

//	@Test
//	public void testGetAmounts() {
//		try {
//			Vector<Double> amounts=new Vector<Double>();
//			String SQL="Select Importe from Movimiento";
//			PreparedStatement p=this.db.prepareStatement(SQL);
//			ResultSet r=p.executeQuery();
//			while (r.next()) {
//				amounts.add(r.getDouble(1));
//			}
//			p.close();
//			System.out.println("amounts.size()=" + amounts.size());
//			assertTrue("Expected 571", amounts.size()==571);
//		} catch (Exception e) {
//			fail("Unexpected exception: " + e.toString());
//		}
//	}

}
